package com.hellobike.haxing.modules.wms.controller;

import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.wms.entity.CodeHolderPoint;
import com.hellobike.haxing.modules.wms.entity.Container;
import com.hellobike.haxing.modules.wms.entity.PickHeader;
import com.hellobike.haxing.modules.wms.entity.Warehouse;
import com.hellobike.haxing.modules.wms.exception.WmsServiceException;
import com.hellobike.haxing.modules.wms.facade.WmsAppPickFacade;
import com.hellobike.haxing.modules.wms.service.IWarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags="APP拣货")
@RestController
@RequestMapping("/wms/app/pick")
@Slf4j
public class PickAppController extends WmsBaseController<Warehouse, IWarehouseService> {

    @Autowired
    private WmsAppPickFacade wmsAppPickFacade;

    @ApiOperation("查询拣货单")
    @PostMapping("/queryPickHeaderByOrderCode")
    public Result<?> queryPickHeaderByOrderCode(@RequestParam String orderCode){
        try {
            PickHeader pickHeader = wmsAppPickFacade.queryPickHeaderByOrderCode(orderCode,getWarehouseId());
            if (pickHeader == null) {
                return Result.error("未查询到该订单:"+orderCode);
            }
            return Result.ok(pickHeader);
        } catch (WmsServiceException e) {
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("查询容器")
    @PostMapping("/queryBox")
    public Result<?> queryBox(@RequestParam String boxCode){
        try {
            Container container = wmsAppPickFacade.queryBox(boxCode,getWarehouseId());
            if (container == null) {
                return Result.error("未查询到该容器:"+boxCode);
            }
            return Result.ok(container);
        } catch (WmsServiceException e) {
            return Result.error(e.getMessage());
        }
    }
    @ApiOperation("按箱拣货")
    @PostMapping("/pickByBoxAndSku")
    public Result<?> pickByBoxAndSku(@RequestParam Long pickHeaderId,
                                                @RequestParam Long boxId,
                                                @RequestParam String barcode){
        try {
            Integer userId = getSysUserId();
            Long warehouseId = getWarehouseId();
            String s = wmsAppPickFacade.pickByBoxAndSku(pickHeaderId, boxId, barcode, warehouseId, userId);
            return Result.ok(s);
        } catch (WmsServiceException e) {
            return Result.error(e.getMessage());
        }
    }
    @ApiOperation("直接拣货")
    @PostMapping("/pickBySku")
    public Result<?> pickBySku(@RequestParam Long pickHeaderId,
                                     @RequestParam String barcode){
        try {
            Integer userId = getSysUserId();
            Long warehouseId = getWarehouseId();
            String s = wmsAppPickFacade.pickBySku(pickHeaderId, barcode, warehouseId, userId);
            return Result.ok(s);
        } catch (WmsServiceException e) {
            return Result.error(e.getMessage());
        }
    }
    @ApiOperation("拣货撤销")
    @PostMapping("/unPick")
    public Result<?> unPick(@RequestParam Long pickHeaderId,
                            @RequestParam(required = false) Long boxId,
                            @RequestParam String barcode){
        try {
            Integer userId = getSysUserId();
            Long warehouseId = getWarehouseId();
            String s = wmsAppPickFacade.unPick(pickHeaderId,boxId,barcode, warehouseId, userId);
            return Result.ok(s);
        } catch (WmsServiceException e) {
            return Result.error(e.getMessage());
        }
    }


    @ApiOperation("拣货完成")
    @PostMapping("/pickCompleted")
    public Result<?> pickCompleted(@RequestParam Long pickHeaderId){
        try {
            Integer userId = getSysUserId();
            Long warehouseId = getWarehouseId();
            wmsAppPickFacade.pickCompleted(pickHeaderId,warehouseId,userId);
            return Result.ok();
        } catch (WmsServiceException e) {
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("查询码托")
    @PostMapping("/queryPoint")
    public Result<?> queryPoint(@RequestParam String pointCode){
        try {
            CodeHolderPoint point = wmsAppPickFacade.queryPoint(pointCode,getWarehouseId());
            if (point == null) {
                return Result.error("未查询到该码托点:"+pointCode);
            }
            return Result.ok(point);
        } catch (WmsServiceException e) {
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("绑定码托")
    @PostMapping("/bindPoint")
    public Result<?> bindPoint(@RequestParam Long pickHeaderId,@RequestParam Long pointId){
        try {
            Integer userId = getSysUserId();
            Long warehouseId = getWarehouseId();
            wmsAppPickFacade.bindPoint(pickHeaderId,pointId,warehouseId,userId);
            return Result.ok();
        } catch (WmsServiceException e) {
            return Result.error(e.getMessage());
        }
    }



}
